home *** CD-ROM | disk | FTP | other *** search
- #include "abc.h"
- #include "Quickdraw.h"
- #include "EventMgr.h"
- #include "WindowMgr.h"
- #include "MenuMgr.h"
- #include "FontMgr.h"
-
- /* printw()
- *
- * Displays strings and numbers in a
- * special window
- *
- * This function is designed to receive
- * a variable number of parameters. The
- * number is computed by the number of
- * percent signs in the control string.
- * If the number of parameters following the
- * control string does not match the
- * number of percent signs, expect
- * the unexpected.
- */
- printw(cs)
- char *cs; /* the control string */
- {
- #define Bufsz 14 /* size of buffer to hold */
- /* converted numbers */
-
- static Rect boundsRect; /* variables for */
- static Rect windowRect; /* defining printw */
- static WindowRecord wrc; /* window, pw is */
- static WindowPtr pw = 0; /* initialized to 0 */
- static short linesz; /* size of line */
-
- WindowPtr oldport; /* save grafport here */
- FontInfo info;
- short nl;
- Point pt;
- RgnHandle updrgn; /* needed for scrolling */
- char numAsStr[Bufsz]; /* number conversion */
- short nsz; /* size of numbers (2 or 4) */
- char **ts; /* ptr to ptr to ctrl string */
- char *ps; /* ptr to parameters */
- ulong num; /* for number conversion */
- short convchar; /* found conversion char */
- short islong; /* number is a long */
- short ishex; /* display in hex digits */
- short isp; /* pascal string or point */
- short isr; /* rectangle */
- Point *ptp;
- Point ***ytp;
- Point *xtp;
- Rect *rtp;
- char c; /* char parameter */
- char *s; /* string pointer parameter */
- long tcs;
- char *tps;
-
- /* Window rectancgle coordinates */
-
- #define wl 0
- #define wr 512
- #define wt 250
- #define wb 342
-
-
- GetPort(&oldport); /* save current graph port */
- if (pw equals 0) /* if window does not exist, */
- { /* open it */
- SetRect(&boundsRect,wl,wt,wr,wb);
- pw = NewWindow(&wrc, &boundsRect,
- CtoPstr(""),True,plainDBox,
- (WindowPtr) -1, True, 0);
- GetFontInfo(&info); /* compute line height */
- linesz = info.ascent + info.descent;
- nl = linesz; /* move down one line as */
- } /* writing will be above */
- else /* boundary. No need to */
- nl = 0; /* move line if already open */
- SetPort(pw); /* Set graf port to this window */
- Move(0,nl); /* Move (relative) */
-
- ts = &cs; /* get address of control string ptr */
- ps = (char *)ts; /* convert to pointer to params */
- ps += sizeof(long); /* skip over control string ptr */
- tcs = (long)cs;
- tps = ps;
- while (*cs) /* loop until end of control string */
- {
- switch (*cs) /* check each character */
- {
- case '%' : /* percent sign: check conversion */
- cs++; /* point to next char */
- convchar = False; /* initialize for conversion loop */
- islong = False;
- ishex = False;
- isp = False;
- isr = False;
- do { /* loop until reach conversion char */
- switch (*cs)
- {
- case 'l' : /* indicates a long */
- islong = True;
- cs++;
- break;
- case 'r' :
- isr = True;
- cs++;
- break;
- case 'p' :
- isp = True;
- cs++;
- break;
- case 'u' : /* unsigned decimal, conversion char */
- case 'd' : /* signed decimal, conversion char */
- if (islong) /* extract number from param */
- {
- num = *(ulong*)ps;
- nsz = sizeof(long);
- }
- else
- {
- num = *(ushort*)ps;
- nsz = sizeof(short);
- }
- ps += nsz; /* point to next param */
- ntoa(num,nsz,'u' - *cs,numAsStr);/* convert number */
- DrawString(CtoPstr(numAsStr)); /* write number */
- convchar = True;
- break;
- case 'x' :
- if (islong) /* extract number from param */
- {
- num = *(ulong*)ps;
- nsz = sizeof(long);
- }
- else
- {
- num = *(ushort*)ps;
- nsz = sizeof(short);
- }
- ntox(num,nsz,numAsStr);
- DrawString(CtoPstr(numAsStr));
- convchar = True;
- ps += nsz; /* point to next param */
- break;
- case 's' :
- s = *(char **)ps;
- if (isp)
- DrawString(s);
- else
- {
- DrawString(CtoPstr(s));
- PtoCstr(s);
- }
- ps += sizeof(char*);
- convchar = True;
- break;
- case 't' :
- if (isp)
- {
- ptp = *(Point **)ps;/*!!!*/
- DrawString ("\pPoint h: ");
- ntoa((ulong)ptp->h,2,True,numAsStr);
- DrawString(CtoPstr(numAsStr));
- DrawString ("\p v: ");
- ntoa((ulong)ptp->v,2,True,numAsStr);
- DrawString(CtoPstr(numAsStr));
- }
- else if (isr)
- {
- rtp = *(Rect**)ps;
- DrawString ("\pRect t: ");
- ntoa ((ulong)rtp->top,2,True,numAsStr);
- DrawString(CtoPstr(numAsStr));
- DrawString("\p l: ");
- ntoa ((ulong)rtp->left,2,True,numAsStr);
- DrawString(CtoPstr(numAsStr));
- DrawString("\p b: ");
- ntoa ((ulong)rtp->bottom,2,True,numAsStr);
- DrawString(CtoPstr(numAsStr));
- DrawString("\p r: ");
- ntoa ((ulong)rtp->right,2,True,numAsStr);
- DrawString(CtoPstr(numAsStr));
- }
- ps += sizeof(char*);
- convchar = True;
- break;
-
- case 'c' :
- c = *(ushort*)ps;
- DrawChar(c);
- nsz = sizeof(short);
- convchar = True;
- ps += nsz;
- break;
- default : /* if it is not any expected char, */
- DrawChar(*cs); /* write the char out and go on */
- convchar = True;
- }
- } while (not convchar);
- break;
- case '\n' : /* newline ('\n') in control string */
- GetPen(&pt); /* find current pen position */
- if (pt.v+linesz > wb-wt)/* if it goes off window, */
- { /* scroll the window */
- updrgn = NewRgn();
- ScrollRect(&(pw->portRect),0,-linesz,updrgn);
- DisposeRgn(updrgn); /* nothing to update */
- Move(0,-linesz); /* move back onto window */
- }
- Move(-pt.h,linesz); /* move to begining of next line */
- break;
- default : /* any other character just gets */
- DrawChar(*cs); /* written on the window */
- }
- cs++; /* move pointer to next char */
- } /* in control string and continue */
-
- SetPort(oldport); /* restore orignal graf port */
- }
-
- /* Convert numbers to ascii strings
- * Handles signed and unsigned
- * short and long values
- * Note: Length of string returned
- * must be large enough to
- * hold -2G (12 bytes)
- */
- ntoa(n,len,issigned,s)
- ulong n; /* number to convert */
- short len; /* size of n (2 or 4)*/
- short issigned;/* signed flag (True = Signed) */
- char *s; /* string to return */
- {
- char ts[12]; /* temporary string */
- int i = 0; /* counter, initialized */
- ulong m; /* working copy of */
- long sm; /* to convert signed values */
-
- if (n equals 0) /* if n is zero, place '0' */
- ts[i++] = '0'; /* in temporary string */
- else
- {
- if (issigned) /* if sign flag is set, */
- { /* convert to signed value */
- if (len equals sizeof(long))
- sm = (long)n;
- else
- sm = (short)n;
- if (issigned = sm < 0) /* Check if value is */
- n = -sm; /* negative. If so, */
- } /* keep the flag and */
- /* get the absolute value */
- while (n) /* Convert number into ascii */
- { /* by repeatedly taking mod */
- ts[i++] = n % 10 + '0'; /* and dividing. This */
- n /= 10; /* gives a string in */
- } /* reverse order */
- if (issigned) /* If number was negative, */
- ts[i++] = '-'; /* stick a minus sign in */
- } /* the string.*/
-
- do { /* Reverse the string */
- *s++ = ts[--i]; /* to the correct direction*/
- }
- while (i);
-
- *s = '\0'; /* Place null terminator on */
- } /* string */
-
- /* Convert numbers to hex strings
- * Handles unsigned
- * short and long values
- * Note: Length of string returned
- * must be large enough to
- * hold 4G (12 bytes)
- */
- ntox(n,len,s)
- ulong n; /* number to convert */
- short len; /* size of n (2 or 4)*/
- char *s; /* string to return */
- {
- int i = 0; /* counter, initialized */
- short nibble; /* one nibble */
-
- static char hexcode[] = {'0','1','2','3','4','5','6','7',
- '8','9','A','B','C','D','E','F'};
-
-
- if (n equals 0) /* if n is zero, place '0' */
- s[i++] = '0'; /* in string */
- else
- {
- i = len *= 2;
- while (len)
- {
- nibble = n & 0x0000000F;
- s[--len] = hexcode[nibble];
- n >>= 4;
- }
- }
- s[i] = 0;
- }
-